package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.service.wx.WxGoodsService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.util.VoUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ClassName: GoodsController
 * @Auther: lyz
 * @Date: 2023/05/19/22:06
 * @Description:
 */
@RestController
@RequestMapping("wx/goods/")
public class WxGoodsController {
    @Autowired
    WxGoodsService goodsService;

    @GetMapping("count")
    public Object goodsCount() {
        Integer count = goodsService.count();
        return ResponseUtil.ok(count);
    }

    @GetMapping("category")
    public Object goodsCategory(Integer id) {
        MarketCategory parent = goodsService.parentCategory(id);
        List<MarketCategory> brother = goodsService.brotherCategory(id);
        MarketCategory current = goodsService.currentCategory(id);
        Map<String, Object> map = new HashMap<>();
        map.put("brotherCategory", VoUtil.objectToVolist(brother, MarketCategory.class));
        map.put("currentCategory", VoUtil.objectVo(current, MarketCategory.class));
        map.put("parentCategory", VoUtil.objectVo(parent, MarketCategory.class));
        return ResponseUtil.ok(map);
    }

    //    @GetMapping("list")
//    public Object goodsList(Integer categoryId, Integer page, Integer limit) {
//        List<MarketGoods> marketGoods = goodsService.goodList(categoryId, page, limit);
//        List<MarketCategory> categoryList = goodsService.categoryList(categoryId, page, limit);
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("filterCategoryList", VoUtil.objectToVolist(categoryList, CategoryVo.class));
//        map.put("limit", limit);
//        map.put("list", VoUtil.objectToVolist(marketGoods, HotGoodsVo.class));
//        map.put("page", page);
//        map.put("pages", (marketGoods.size() / limit) + 1);
//        map.put("total", marketGoods.size());
//        Object o = ResponseUtil.ok(map);
//        return o;
//    }
//    @GetMapping("list")
//    public Object goodsList(Integer categoryId, Integer page, Integer limit) {
//        Map<String, Object> map = goodsService.goodList(categoryId, page, limit);
////        map.put("filterCategoryList", VoUtil.objectToVolist(categoryList, CategoryVo.class));
////        map.put("limit", limit);
////        map.put("list", VoUtil.objectToVolist(marketGoods, HotGoodsVo.class));
////        map.put("page", page);
////        map.put("pages", (marketGoods.size() / limit) + 1);
////        map.put("total", marketGoods.size());
//        Object o = ResponseUtil.ok(map);
//        return o;
//    }
    @GetMapping("list")
    public Object goodsList(String keyword, Integer page, Integer limit, String sort, String order, Integer categoryId, Integer brandId, boolean isNew, boolean isHot) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        Map<String, Object> map = goodsService.goodList(keyword, categoryId, page, limit, sort, order, userId, brandId, isNew, isHot);
        return ResponseUtil.ok(map);
    }
}
